﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Jayden.Dll.Core.Persistence;
using System.Collections.ObjectModel;
using Jayden.Dll.Core.Persistence.Attributes;

namespace Jayden.Dll.Entities.Common
{
	[Table]
	public class Invoice : ObjectBase, IComparable<Invoice>
	{
		private DateTime m_CreationDate;
		private string m_Email;
		private string m_BillingAddressLastName;
		private string m_BillingAddressFirstName;
		private string m_BillingAddressStreet;
		private string m_BillingAddressNumber;
		private string m_BillingAddressBox;
		private string m_BillingAddressPostCode;
		private string m_BillingAddressCity;
		private string m_BillingAddressCountry;

		private string m_DeliveryAddressLastName;
		private string m_DeliveryAddressFirstName;
		private string m_DeliveryAddressStreet;
		private string m_DeliveryAddressNumber;
		private string m_DeliveryAddressBox;
		private string m_DeliveryAddressPostCode;
		private string m_DeliveryAddressCity;
		private string m_DeliveryCountry;

		private List<InvoiceLine> m_Lines;

		public Invoice(UnitOfWork uow) : base(uow, 0) { }

		[Field(PrimaryKey = true, AutoIncrement = true)]
		public long InvoiceId { get { return (long)ObjectId[0]; } set { ObjectId[0] = value; } }
		[Field]
		public DateTime CreationDate { get { return m_CreationDate; } set { m_CreationDate = value; } }
		[Field]
		public string Email { get { return m_Email; } set { m_Email = value; } }
		[Field]
		public string BillingAddressLastName { get { return m_BillingAddressLastName; } set { m_BillingAddressLastName = value; } }
		[Field]
		public string BillingAddressFirstName { get { return m_BillingAddressFirstName; } set { m_BillingAddressFirstName = value; } }
		[Field]
		public string BillingAddressStreet { get { return m_BillingAddressStreet; } set { m_BillingAddressStreet = value; } }
		[Field]
		public string BillingAddressNumber { get { return m_BillingAddressNumber; } set { m_BillingAddressNumber = value; } }
		[Field]
		public string BillingAddressBox { get { return m_BillingAddressBox; } set { m_BillingAddressBox = value; } }
		[Field]
		public string BillingAddressPostCode { get { return m_BillingAddressPostCode; } set { m_BillingAddressPostCode = value; } }
		[Field]
		public string BillingAddressCity { get { return m_BillingAddressCity; } set { m_BillingAddressCity = value; } }
		[Field]
		public string BillingAddressCountry { get { return m_BillingAddressCountry; } set { m_BillingAddressCountry = value; } }
		[Field]
		public string DeliveryAddressLastName { get { return m_DeliveryAddressLastName; } set { m_DeliveryAddressLastName = value; } }
		[Field]
		public string DeliveryAddressFirstName { get { return m_DeliveryAddressFirstName; } set { m_DeliveryAddressFirstName = value; } }
		[Field]
		public string DeliveryAddressStreet { get { return m_DeliveryAddressStreet; } set { m_DeliveryAddressStreet = value; } }
		[Field]
		public string DeliveryAddressNumber { get { return m_DeliveryAddressNumber; } set { m_DeliveryAddressNumber = value; } }
		[Field]
		public string DeliveryAddressBox { get { return m_DeliveryAddressBox; } set { m_DeliveryAddressBox = value; } }
		[Field]
		public string DeliveryAddressPostCode { get { return m_DeliveryAddressPostCode; } set { m_DeliveryAddressPostCode = value; } }
		[Field]
		public string DeliveryAddressCity { get { return m_DeliveryAddressCity; } set { m_DeliveryAddressCity = value; } }
		[Field]
		public string DeliveryCountry { get { return m_DeliveryCountry; } set { m_DeliveryCountry = value; } }

		public ReadOnlyCollection<InvoiceLine> Lines
		{
			get
			{
				if (m_Lines == null)
				{
					InvoiceLineDataAccess dataAccess = UnitOfWork.DataAccessManager.GetDataAccess<InvoiceLine>() as InvoiceLineDataAccess;
					if (dataAccess != null)
						m_Lines = dataAccess.ReadByInvoiceId(InvoiceId);
				}
				return m_Lines.AsReadOnly();
			}
		}

		public int CompareTo(Invoice other)
		{
			return m_CreationDate.CompareTo(other.m_CreationDate);
		}
	}
}
